package cn.ad.server.common.utils;

import java.util.List;

import org.springframework.util.ObjectUtils;

/**
 * @author dongbin.yu
 * @from 2016-08-10
 * @since V1.0
 */
public class StringUtil {

    public static String arrayToDelimitedString(int[] arr, String delim) {
        if (ObjectUtils.isEmpty(arr)) {
            return "";
        }
        if (arr.length == 1) {
            return ObjectUtils.nullSafeToString(arr[0]);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }
    public static String arrayToDelimitedString(Integer[] arr, String delim) {
    	if (ObjectUtils.isEmpty(arr)) {
    		return "";
    	}
    	if (arr.length == 1) {
    		return ObjectUtils.nullSafeToString(arr[0]);
    	}
    	StringBuilder sb = new StringBuilder();
    	for (int i = 0; i < arr.length; i++) {
    		if (i > 0) {
    			sb.append(delim);
    		}
    		sb.append(arr[i]);
    	}
    	return sb.toString();
    }

    public static String arrayToCommaDelimitedString(int[] arr) {
        return arrayToDelimitedString(arr, ",");
    }
    
    public static String arrayToCommaDelimitedString(List<Integer> arr) {
    	Integer[] i = new Integer[arr.size()];
    	Integer[] array = arr.toArray(i);
    	return arrayToDelimitedString(array, ",");
    }

}
